function S = init_setup()
S.cols = 10;
S.rows = 21;
S.n_pieces = 7;
S.n_orients = 4;
S.lost = false;
S.turn = 0;
S.cleared = 0;
S.field = zeros(S.rows, S.cols);
S.top = zeros(1, S.cols);
S.nextPiece = floor(rand()*S.n_pieces);
S.legalMoves = cell(1, S.n_pieces);
S.orient = 0;
S.slot = 1;
S.pOrients = [1,2,4,4,4,2,2];

S.pWidth = cell(1, S.n_pieces);
S.pWidth{1} = [2];
S.pWidth{2} = [1,4];
S.pWidth{3} = [2,3,2,3];
S.pWidth{4} = [2,3,2,3];
S.pWidth{5} = [2,3,2,3];
S.pWidth{6} = [3,2];
S.pWidth{7} = [3,2];

S.pHeight = cell(1, S.n_pieces);
S.pHeight{1} = [2];
S.pHeight{2} = [4,1];
S.pHeight{3} = [3,2,3,2];
S.pHeight{4} = [3,2,3,2];
S.pHeight{5} = [3,2,3,2];
S.pHeight{6} = [2,3];
S.pHeight{7} = [2,3];

S.pBottom = cell(S.n_pieces, S.n_orients);
S.pBottom{1,1} = [0,0];
S.pBottom{2,1} = [0];
S.pBottom{2,2} = [0,0,0,0];
S.pBottom{3,1} = [0,0];
S.pBottom{3,2} = [0,1,1];
S.pBottom{3,3} = [2,0];
S.pBottom{3,4} = [0,0,0];
S.pBottom{4,1} = [0,0];
S.pBottom{4,2} = [0,0,0];
S.pBottom{4,3} = [0,2];
S.pBottom{4,4} = [1,1,0];
S.pBottom{5,1} = [0,1];
S.pBottom{5,2} = [1,0,1];
S.pBottom{5,3} = [1,0];
S.pBottom{5,4} = [0,0,0];
S.pBottom{6,1} = [0,0,1];
S.pBottom{6,2} = [1,0];
S.pBottom{7,1} = [1,0,0];
S.pBottom{7,2} = [0,1];

S.pTop = cell(S.n_pieces, S.n_orients);
S.pTop{1,1} = [2,2];
S.pTop{2,1} = [4];
S.pTop{2,2} = [1,1,1,1];
S.pTop{3,1} = [3,1];
S.pTop{3,2} = [2,2,2];
S.pTop{3,3} = [3,3];
S.pTop{3,4} = [1,1,2];
S.pTop{4,1} = [1,3];
S.pTop{4,2} = [2,1,1];
S.pTop{4,3} = [3,3];
S.pTop{4,4} = [2,2,2];
S.pTop{5,1} = [3,2];
S.pTop{5,2} = [2,2,2];
S.pTop{5,3} = [2,3];
S.pTop{5,4} = [1,2,1];
S.pTop{6,1} = [1,2,2];
S.pTop{6,2} = [3,2];
S.pTop{7,1} = [2,2,1];
S.pTop{7,2} = [2,3];

for i = 0:S.n_pieces-1
    n = 0;
    for j = 0:S.pOrients(i+1)-1
        n = n + S.cols + 1 - S.pWidth{i+1}(j+1);
    end
    S.legalMoves{i+1} = zeros(n,2);
    n = 0;
    for j = 0:S.pOrients(i+1)-1
        for k = 0:(S.cols+1-S.pWidth{i+1}(j+1)-1)
            S.legalMoves{i+1}(n+1,S.orient+1) = j;
            S.legalMoves{i+1}(n+1,S.slot+1) = k;
            n = n+1;
        end
    end
end

end